﻿using System;
using System.Collections.Generic;

namespace System.Linq
{
	public static class EnumerableExternsion
	{
		public static bool IsEmpty<T>(this IEnumerable<T> source)
		{
			return source == null || !source.Any<T>();
		}

		public static bool IsValid<T>(this IEnumerable<T> source)
		{
			return source != null && source.Any<T>();
		}

		public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
		{
			if (source.IsEmpty<T>() || action == null)
			{
				return;
			}
			source.ToList<T>().ForEach(action);
		}

		public static IEnumerable<TResult> ForEach<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> func)
		{
			if (!source.IsEmpty<TSource>())
			{
				return source.Select(func);
			}
			return null;
		}

		public static T FirstOrNull<T>(this IEnumerable<T> source) where T : class
		{
			return source.DefaultIfEmpty(default(T)).FirstOrDefault<T>();
		}

		public static IEnumerable<T> Except<T>(this IEnumerable<T> source, params T[] elements)
		{
			return source.Except(elements.AsEnumerable<T>());
		}

		public static bool Contains<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> matchFun)
		{
			return source.Any(matchFun);
		}
	}
}
